<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class LiuController extends Controller
{
    

    public function getIndex(Request $request)
	{
        
		$data=DB::table('liuyan')
					->where("subject","like","%".$request->input('search')."%" )
					->orderBy('id','desc')
					->paginate($request->input('pages',10));
		$data1=$data->all();
		// dd($data);	
		foreach($data1 as $k =>$v)
		{
			$data[$k]->time=date("Y年m月d日",$v->time);
			// $data[$k]->content=strip_tags($v->content);
		}
		
		
		// dd($request->all());
		return view('admin.liuyan.index',['data'=>$data,'request'=>$request->all()]);
	}



    public function add()
    {
    	return view('admin.liuyan.add');
    }
    
public function postUpdatest(Request $request)
    {

    	$id=$request->id;
    	
    	$data=DB::table('liuyan')->select('status')->where('id',$id)->first();

    	if($data->status==0)
    	{
    		// return 2222;
    		$res=DB::table('liuyan')->where('id',$id)->update(['status'=>1]);
    		if($res)
    		{
    			return response()->json(1);
    		}else
    		{
    			return response()->json(5);
    		}
    	}elseif($data->status==1)
    	{
    		// return 1111;
    		$res=DB::table('liuyan')->where('id',$id)->update(['status'=>0]);
    		if($res)
    		{

    			return response()->json(0);
    		}else
    		{
    			return response()->json(5);
    		}
    		
    	}
    }


//双击修改name
    public function updatena(Request $request)
    {
    	// return $request->all();
    	$id=$request->id;
    	$name=$request->name;
    	//检测名是否重复
    	$res=DB::table('liuyan')->where('name',$name)->first();
    	if($res)
    	{
    		return response()->json(5);
    	}
    	//插入
    	$res=DB::table('liuyan')->where('id',$id)->update(['name'=>$name]);
    	if($res)
    	{
    		return response()->json(1);
    	}else
    	{
    		return response()->json(0);
    	}



    }
//双击修改name
//编辑
	public function edit($id)
	{
		$data=DB::table('liuyan')->where('id',$id)->first();
		
		if($data)
		{
			// echo 111;
			return view('admin.liuyan.edit',['data'=>$data]);
		}else
		{

			return back()->with(['info'=>'编辑错误']);
		}

	}
//编辑
//编辑后更新
	public function updateall(Request $request)
	{
		$id=$request->id;
		$data=$request->except('_token','id');
		$data['time']=time();
		
		$res=DB::table('liuyan')->where('id',$id)->update($data);
		if($res)
		{
			return redirect('admin/liuyan/index')->with(['info'=>'修改成功']);
		}else
		{
			return redirect('admin/liuyan/index')->with(['info'=>'修改失败']);
		}


	}
//编辑后更新
//删除
   public function getDelete($id)
   {
   	
   		$res=DB::table('liuyan')->delete($id);
   		if($res)
   		{
   			return back()->with(['info'=>'删除成功']);
   		}else
   		{
   			return back()->with(['info'=>'删除失败']);

   		}
   }



}
